package com.landian.commons.base.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 轻一点的返回结果
 * @author caojl
 * @date: 2018-08-04
 * @time: 17:45
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Result<T> implements Serializable {

    private static final int SUCCESS_CODE = 0;
    private static final int DEFAULT_ERROR_CODE = -1;

    /**
     * 数据，真正的结果对象
     */
    T data;
    /**
     * 错误码
     */
    private Integer errorCode;
    /**
     * 错误信息
     */
    private String errorMsg;

    public static <T> Result<T> success(T model){
        return new Result(model, SUCCESS_CODE, "");
    }

    public static <T> Result<T> fail(int errorCode, String errorMsg,T model){
        return new Result(model,errorCode,errorMsg);
    }

    public static Result fail(String errorMsg){
        return Result.builder().errorCode(DEFAULT_ERROR_CODE).errorMsg(errorMsg).build();
    }

    public static Result fail(int errorCode, String errorMsg){
        return Result.builder().errorCode(errorCode).errorMsg(errorMsg).build();
    }
}
